package cn.yitun.chat.yitunserverimapicore.business.group.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupMemberDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupNumberDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupRelationDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.group.GroupInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupCategoryVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.enums.groupChat.GroupMemberPositionEnums;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunservercommonlib.util.codec.IdGen;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupService;
import cn.yitun.chat.yitunserverimbase.dao.group.*;
import cn.yitun.chat.yitunserverimbase.dao.system.UserDao;
import cn.yitun.chat.yitunserverimcore.general.main.biz.group.manager.GroupCategoryManager;
import cn.yitun.chat.yitunserverimcore.general.main.biz.group.manager.GroupMemberManager;
import cn.yitun.chat.yitunserverimcore.general.main.push.group.GroupInfoPush;
import cn.yitun.chat.yitunserverimcore.general.main.push.group.GroupRelationPush;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * Date 2019-01-21 11:39:20<br>
 * Description
 * @author yitun
 * @since 1.0.0
 */
@Service
public class GroupServiceImpl
		extends CrudServiceImpl<GroupDao, GroupDto, GroupInfo, GroupVo>
		implements GroupService {

	@Resource
	private GroupMemberDao groupMemberDAO;
	@Resource
	private GroupCategoryDao groupCategoryDAO;
	@Resource
	private GroupRelationDao groupCategoryMemberDAO;
	@Resource
	private GroupCategoryManager groupCategoryManager;
	@Resource
	private GroupMemberManager groupMemberManager;
	@Resource
	private GroupNumberDao numberDAO;
	@Resource
	private GroupInfoPush groupInfoPush;
	@Resource
	private GroupRelationPush groupRelationPush;

	@Resource
	private UserDao userDao;

	/**
	 * 通过唯一编码获取
	 */
	public GroupVo getGroupByCode(String code) {
		return dao.getGroupByCode(code);
	}

	/**
	 * 更新
	 */
	@Transactional
	public Info update(Long key, Long userId, GroupDto group) {
		Info info = new Info();
		if (null != group && NumUtil.isValid(group.getId())) {
			Long groupId = group.getId();
			String inviterUserPosition = groupMemberManager.getPosition(groupId, userId);
			boolean isOwner = groupMemberManager.isOwner(inviterUserPosition);
			if (isOwner) {
				dao.update(group);
				List<Long> userIds = groupMemberManager.getGroupMemberUserIdList(groupId);
				groupInfoPush.pushUpdate(IdGen.getNumber(), groupId, userIds);
			}
		}
		return info;
	}

	/**
	 * 更新名称，头像，介绍
	 */
	@Transactional
	@Override
	public ResultMessage updateGroupInfo(Long key, Long userId, GroupDto group) {
		if (null != group && NumUtil.isValid(group.getId())) {
			Long groupId = group.getId();
			String inviterUserPosition = groupMemberManager.getPosition(groupId, userId);
			boolean isOwner = groupMemberManager.isOwner(inviterUserPosition);
			if (isOwner) {
				dao.updateGroupInfo(group);
				List<Long> userIds = groupMemberManager.getGroupMemberUserIdList(groupId);
				groupInfoPush.pushUpdate(IdGen.getNumber(), groupId, userIds);
			}
		}
		return new ResultMessage(R.param("group", group));
	}

	/**
	 * 获取列表 剔除已经关联的群组
	 */
	public List<GroupVo> queryGroupList(GroupDto groupQuery) {
		List<GroupVo> groupList = dao.queryGroupList(groupQuery);
		return groupList;
	}

	/**
	 * 新增
	 */
	@Transactional
	public ResultMessage add(Long key, Long userId, GroupDto group) {
		ResultMessage message = new ResultMessage();

		//检查是否有重复群名称
		if (dao.count(YitunMapUtil.java2Map(group, false)) > 0) {
			message.addError(ExceptionEnum.NAME_IS_REPETITION);
			return message;
		}
		//生成数子账号
		GroupNumberDto groupNumber = new GroupNumberDto();
		groupNumber.setCode(IdGen.uuid());
		numberDAO.save(groupNumber);
		//保存群信息
		group.setCode(groupNumber.getCode());
		if (null == group.getHead()) {
			long i = new Random().nextLong();
			i = i + 1;
			group.setHead(i);
		}
		group.setCreateBy(userId);
		group.setUpdateBy(userId);
		dao.save(group);

		GroupCategoryVo groupCategory = groupCategoryDAO.getVO(group.getCategoryId());
		if (groupCategory == null)
			groupCategory = groupCategoryManager.getOrCreateDefault(userId);
		//添加群分类成员
		GroupRelationDto groupRelation = new GroupRelationDto();
		groupRelation.setUserId(userId);
		groupRelation.setGroupId(group.getId());
		groupRelation.setCategoryId(groupCategory.getId());
		groupRelation.setCreateBy(userId);
		groupRelation.setUpdateBy(userId);
		groupRelation.setCategoryId(groupCategory.getId());
		groupCategoryMemberDAO.save(groupRelation);
		UserVo userVo = userDao.getVO(userId);
		//添加群主
		GroupMemberDto groupMember = new GroupMemberDto();
		groupMember.setUserId(userId);
		groupMember.setNickname(userVo.getNickname());
		groupMember.setGroupId(group.getId());
		groupMember.setPosition(GroupMemberPositionEnums.POSITION_OWNER.getType());
		groupMember.setCreateBy(userId);
		groupMember.setUpdateBy(userId);
		groupMemberDAO.save(groupMember);

		// TODO 推送新增群信息
//	 	groupPush.pushAdd(IdGen.getNumber(), group, userId);

		groupRelationPush.pushAdd(IdGen.getNumber(), group.getId(), userId);

		message.bodyPut("group", group);
		return message;
	}

	public List<GroupVo> getGroupList(List<Long> groupIds) {
		return dao.getGroupList(groupIds);
	}
}
